package cn.edu.zucc.util;

import java.util.HashMap;
import java.util.Map;

public class PageUtil {
    //一共多少页
    private int totalPage;
    //当前页码
    private int currentPage;
    //返回的页码数
    private Map<String,Integer> pageNum = new HashMap<>();

    public Map<String,Integer> PageTool(int page, int totalRecord, int size){
        //计算总页数
        totalPage = totalRecord/size;
        if (totalRecord%size!=0){
            totalPage = totalPage + 1;
        }
        if (page<1){
            page = 1;
        }
        if (page>totalPage){
            page = totalPage;
        }
        //处理完成的请求页码
        currentPage = page;
        //在这里计算页码的开始位置和结束位置
        int startPage = 1;
        int endPage = totalPage;
        if (totalPage>10){
            if (page>6){
                startPage = page-5;
                endPage = page+4;
                if(endPage>totalPage){
                    endPage = totalPage;
                }
            }else {
                endPage = 10;
            }
        }
        pageNum.put("startPage",startPage);
        pageNum.put("endPage",endPage);
        pageNum.put("currentPage",currentPage);
        return pageNum;
    }
}
